//
//  PrescriptionOrderCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/2.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol PrescriptionOrderCellCellDelegate: NSObjectProtocol {
    func pay(_ cell: PrescriptionOrderCell)
    func cancel(_ cell: PrescriptionOrderCell)
    func applyRefund(_ cell: PrescriptionOrderCell)
    func checkLogistics(_ cell: PrescriptionOrderCell)
    func confirmReceive(_ cell: PrescriptionOrderCell)
    func cancelRefund(_ cell: PrescriptionOrderCell)
    func inputExpressNumber(_ cell: PrescriptionOrderCell)
}

class PrescriptionOrderCell: UITableViewCell {
    
    weak var delegate: PrescriptionOrderCellCellDelegate?
    
    private var viewModel: PrescriptionOrderCellViewModel?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
    
    func bind(to viewModel: PrescriptionOrderCellViewModel) {
        self.viewModel = viewModel
        avatarView.kf.setImage(with: URL(string: viewModel.url),
                               placeholder: UIImage(named: "avatar_default"))
        nameLabel.text = viewModel.doctorName
        departmentLabel.text = viewModel.departmentName
        drugLabel.text = viewModel.drugsName
        costLabel.text = viewModel.totalCost
        
        setupButton(with: viewModel)
        
        guard let status = viewModel.status else { return }
        
        statusLabel.text = viewModel.statusName
        statusLabel.textColor = status.statusTextColor
        bottomLabel.text = status.rightTitle
        
        bottomLine.isHidden = viewModel.bottomHidden
        stackView.isHidden = viewModel.bottomHidden
        styleLabel.isHidden = status.styleLabelHidden
        
        bottomLabel.isHidden = status.bottomLabelHidden
        bottomLabel.text = status.bottomLabelText
        
        switch status {
        case .waitPay:
            styleLabel.attributedText = viewModel.remainPayTime
        default:
            styleLabel.textColor = .textLight
            styleLabel.text = "配送方式：\(viewModel.receiveType.wayText)"
        }
    }
    
    private func setupButton(with viewModel: PrescriptionOrderCellViewModel) {
        guard let status = viewModel.status else { return }
        
        leftButton.isHidden = viewModel.leftIsHidden
        leftButton.setTitle(status.leftTitle, for: .normal)
        
        rightButton.isHidden = status.rightIsHidden
        rightButton.setTitle(status.rightTitle, for: .normal)
        
        centerButton.isHidden = status.centerIsHidden
        centerButton.setTitle(status.centerTitle, for: .normal)
    }
    
    @IBOutlet private weak var containerView: UIView! {
        didSet { containerView.layer.cornerRadius = 8 }
    }
    
    @IBOutlet private weak var avatarView: UIImageView! {
        didSet {
            avatarView.contentMode = .scaleAspectFill
            avatarView.layer.cornerRadius = avatarView.height * 0.5
        }
    }
    
    @IBOutlet private weak var nameLabel: UILabel!
    
    @IBOutlet private weak var departmentLabel: UILabel! {
        didSet {
            departmentLabel.textColor = .mainColor
            departmentLabel.font = .regular_14
        }
    }
    
    @IBOutlet private weak var statusLabel: UILabel!
    
    @IBOutlet private weak var drugLabel: UILabel!
    
    @IBOutlet private weak var bottomLabel: UILabel!
    
    @IBOutlet private weak var costLabel: UILabel!
    
    @IBOutlet private weak var styleLabel: UILabel!
    
    @IBOutlet private weak var bottomLine: UIView!
    
    @IBOutlet private weak var stackView: UIStackView!
    
    @IBOutlet private weak var rightButton: UIButton! {
        didSet {
            rightButton.layer.borderWidth = 1
            rightButton.layer.borderColor = UIColor.mainColor.cgColor
            rightButton.layer.cornerRadius = rightButton.height / 2
            rightButton.setTitleColor(.main, for: .normal)
            rightButton.setBackgroundImage(UIImage(color: .white), for: .normal)
            rightButton.setBackgroundImage(UIImage(color: .background), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var leftButton: UIButton! {
        didSet {
            leftButton.layer.borderWidth = 1
            leftButton.layer.borderColor = UIColor.textLight.cgColor
            leftButton.layer.cornerRadius = leftButton.height / 2
            leftButton.setTitleColor(.textLight, for: .normal)
            leftButton.setBackgroundImage(UIImage(color: .white), for: .normal)
            leftButton.setBackgroundImage(UIImage(color: .background), for: .highlighted)
        }
    }
    
    @IBOutlet private weak var centerButton: UIButton! {
        didSet {
            centerButton.layer.borderWidth = 1
            centerButton.layer.borderColor = UIColor.textLight.cgColor
            centerButton.layer.cornerRadius = centerButton.height / 2
            centerButton.setTitleColor(.textLight, for: .normal)
            centerButton.setBackgroundImage(UIImage(color: .white), for: .normal)
            centerButton.setBackgroundImage(UIImage(color: .background), for: .highlighted)
        }
    }
    
    @IBAction private func rightButtonAction(_ sender: UIButton) {
        guard let viewModel = viewModel, let status = viewModel.status else { return }
        
        switch status {
        case .waitPay:              delegate?.pay(self)
        case .shipped:              delegate?.confirmReceive(self)
        case .applyRefund,
             .waitShipApplyRefund,
             .shippedApplyRefund,
             .completeApplyRefund,
             .waitTakeApplyRefund:  delegate?.cancelRefund(self)
        case .waitSendOut,
             .waitSendOutUnreceived,
             .waitSendOutReceived:  delegate?.inputExpressNumber(self)
        default: break
        }
    }
    
    @IBAction private func leftButtonAction(_ sender: UIButton) {
        guard let viewModel = viewModel, let status = viewModel.status else { return }
        
        switch status {
        case .waitPay:      delegate?.cancel(self)
        case .waitShip,
             .shipped,
             .waitTake,
             .completed:    delegate?.applyRefund(self)
        case .waitSendOut,
             .waitSendOutUnreceived,
             .waitSendOutReceived:  delegate?.cancelRefund(self)
        default: break
        }
    }
    
    @IBAction private func centerButtonAction(_ sender: UIButton) {
        delegate?.checkLogistics(self)
    }
}
